//! Run with
//!
//! ```not_rust
//! cargo run -p example-sqlite
//! ```

use crate::web::App;
use fern::Dispatch;

use std::fs::{self, File};

mod users;
mod web;
mod download;
mod util;
mod lolcate;
mod config;
mod cli;
mod logdb;
mod elfinder;
mod directories;
mod rbac;

// 初始化日志
fn init_logger() {
    fs::create_dir_all("log").expect("failed to create log dir");
    let log_file = File::create("log/sheisright.log").expect("Failed to create log file");

    Dispatch::new()
       .format(|out, message, record| {
            out.finish(format_args!(
                "[{}][{}] {}",
                chrono::Local::now().format("%Y-%m-%d %H:%M:%S"),
                record.level(),
                message
            ))
        })
       .level(log::LevelFilter::Warn)
       .chain(log_file)
       .chain(std::io::stdout())
       .apply()
       .expect("Failed to initialize logger");
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 初始化日志
    init_logger();

    App::new().await?.serve().await
}

/* ---------------- 单元测试 ---------------- */
#[cfg(test)]
mod tests {
    use crate::web::App;   // 注意路径
    use axum::body::Body;
    use http::{Request, StatusCode};
    use tower::ServiceExt;

    #[tokio::test]
    async fn hello_world() {
        // 用你的 App::new() 生成 Router
        let app = App::new().await.unwrap().build_router().await;  // 假设 App 有 into_router()

        let resp = app
            .oneshot(Request::builder().uri("/login").body(Body::empty()).unwrap())
            .await
            .unwrap();

        assert_eq!(resp.status(), StatusCode::OK);
    }
}